package jd.se.returnrequest;

import jd.se.common.CurrentUser;
import jd.se.common.Result;
import jd.se.common.Utils;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Map;

@WebServlet({
        "/sendRequest",
        "/showRequest",
        "/confirmRequest",
        "/refuseRequest"
})
public class ReturnRequestAPI extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
        String path = request.getServletPath();
        if ("/sendRequest".equals(path)){
            request.setCharacterEncoding("UTF-8");
            // 获取当前用户对象
            CurrentUser currentUser = (CurrentUser)request.getSession().getAttribute(CurrentUser.SESSION_ATTR_NAME);
            String uId = currentUser.getUserId();
            String toId = request.getParameter("u_id");
            String bId = request.getParameter("b_id");
            Timestamp d= new Timestamp(System.currentTimeMillis());
            ReturnRequest returnRequest = new ReturnRequest(uId,toId,d,bId);
            ReturnRequestService returnRequestService = new ReturnRequestService();
            returnRequestService.insertRequest(returnRequest);
        }
        if ("/showRequest".equals(path)) {
            CurrentUser currentUser = (CurrentUser)request.getSession().getAttribute(CurrentUser.SESSION_ATTR_NAME);
            String to_id = currentUser.getUserId();
            ReturnRequestService returnRequestService = new ReturnRequestService();
            Map<String, Object> requestMap = returnRequestService.getReturnRequest(to_id);
            Utils.outResult(resp, Result.success(requestMap));
        }
        if ("/confirmRequest".equals(path)) {
            String uId = request.getParameter("u_id");
            String bId = request.getParameter("b_id");
            CurrentUser currentUser = (CurrentUser)request.getSession().getAttribute(CurrentUser.SESSION_ATTR_NAME);
            String to_id = currentUser.getUserId();
            BInfo bInfo = new BInfo(uId,bId,to_id);
            ReturnRequestService returnRequestService = new ReturnRequestService();
            returnRequestService.confirmRequest(bInfo);
        }
        if("/refuseRequest".equals(path)){
            String from_id = request.getParameter("from_id");
            String bId = request.getParameter("b_id");
            CurrentUser currentUser = (CurrentUser)request.getSession().getAttribute(CurrentUser.SESSION_ATTR_NAME);
            String to_id = currentUser.getUserId();
            BInfo bInfo = new BInfo(from_id,bId,to_id);
            ReturnRequestService returnRequestService = new ReturnRequestService();
            returnRequestService.refuseRequest(bInfo);
        }
    }
}
